package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.BankexistException;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.entity.BankInfoTB;
import com.wanjia.seal.manager.mapper.BankInfoTBMapper;
import com.wanjia.seal.manager.mapper.CodeTBMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
@Api(tags = "机构管理")
@Slf4j
public class BankInfoTBController extends BaseController{

    @Autowired
    BankInfoTBMapper bankInfoTBMapper;
    @Autowired
    CodeTBMapper codeTBMapper;

    @ResponseBody
    @PostMapping("/bankinfo")
    @ApiOperation(value = "添加机构", notes = "添加是否成功")
    @LoginToken
    public JsonResult createBankInfoTB(@RequestBody BankInfoTB bankInfoTB){
        String bankID = bankInfoTB.getBankID();
        String bankName = bankInfoTB.getBankName();
        BankInfoTB bank = bankInfoTBMapper.selectById(bankID);
        if (bank!=null){
             throw new BankexistException("用户数据已存在");
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("BANKNAME",bankName);
        BankInfoTB bankInfoTB1 = bankInfoTBMapper.selectOne(queryWrapper);
        if (bankInfoTB1!=null){
            throw new BankexistException("用户数据已存在");
        }
        String bankLevel = bankInfoTB.getBankLevel();
        String superBank = bankInfoTB.getSuperBank();
        bankInfoTB.setBankID(bankID);
        bankInfoTB.setBankName(bankName);
        bankInfoTB.setBankLevel(bankLevel);
        bankInfoTB.setSuperBank(superBank);
        bankInfoTBMapper.insert(bankInfoTB);
        return new JsonResult(OK);
    }

    @ResponseBody
    @DeleteMapping("/bankinfo/id")
    @ApiOperation(value = "删除机构", notes = "删除是否成功")
    @LoginToken
    public JsonResult deleteBankInfoTB(String BankID){
        BankInfoTB bankInfoTB = bankInfoTBMapper.selectById(BankID);
        if (bankInfoTB==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        bankInfoTBMapper.deleteById(BankID);
        return new JsonResult(OK);
    }


    @ResponseBody
    @GetMapping("/bankinfo/id")
    @ApiOperation(value = "用ID查询机构", notes = "查询ID是否成功")
    @LoginToken
    public JsonResult<BankInfoTB> selectBankInfoTB(String BankID){
        BankInfoTB bankInfoTB = bankInfoTBMapper.selectById(BankID);
        /*if (bankInfoTB==null){
            throw new DataNotFoundException("数据不存在的异常");
        }*/
        return new JsonResult<>(OK,bankInfoTB);
    }

    @ResponseBody
    @GetMapping("/bankinfos/id")
    @ApiOperation(value = "用ID筛选查询机构", notes = "筛选查询ID是否成功")
    @LoginToken
    public JsonResult selectBankInfoID(String BankID){
        List<BankInfoTB> bankInfoTBS = bankInfoTBMapper.selectByBankID(BankID);
        log.info("bank是:{}",BankID);
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("bankinfo",bankInfoTBS);
        return new JsonResult(OK,jsonObject);
    }

    @ResponseBody
    @GetMapping("/bankinfos")
    @ApiOperation(value = "查询全部机构", notes = "查询是否成功")
    @LoginToken
    public JsonResult<Object> selectsBankInfoTB(String BankID,String SuperBank,Integer pageNum, Integer pageSize){
        List<BankInfoTB> bankInfoTBS = bankInfoTBMapper.SelectBankInfoTBPro(BankID,SuperBank,pageNum, pageSize);
        Integer rows = bankInfoTBMapper.selectByByBankIDCount(BankID,SuperBank);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("BankInfoTB",bankInfoTBS);
        return new JsonResult<>(OK,jsonObject);
    }

    @ResponseBody
    @PutMapping("/bankinfo/id")
    @ApiOperation(value = "更新机构", notes = "更新是否成功")
    @LoginToken
    public JsonResult updateBankInfoTB(@RequestBody BankInfoTB bankInfoTB){
        String bankID = bankInfoTB.getBankID();
        String bankName = bankInfoTB.getBankName();
        String bankLevel = bankInfoTB.getBankLevel();
        String superBank = bankInfoTB.getSuperBank();
        BankInfoTB bankInfoTB1 = bankInfoTBMapper.selectById(bankID);
        if (bankInfoTB1==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        bankInfoTB1.setBankName(bankName);
        bankInfoTB1.setBankLevel(bankLevel);
        bankInfoTB1.setSuperBank(superBank);
        bankInfoTBMapper.updateById(bankInfoTB1);
        return new JsonResult(OK);
    }

    @ResponseBody
    @GetMapping("/bankinfo/parent")
    @ApiOperation(value = "查询父机构", notes = "查询是否成功")
    @LoginToken
    public JsonResult<Object> selectsSuperBankInfoTB(String SuperBank, Integer pageNum, Integer pageSize){
        List<BankInfoTB> bankInfoTBS = bankInfoTBMapper.SelectsSuperBankInfoTB(SuperBank, pageNum, pageSize);
        Long rows = bankInfoTBMapper.selectCount(null);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("BankInfoTB",bankInfoTBS);
        return new JsonResult<>(OK,jsonObject);
    }

}
